package au.edu.swin.swinvehicle.driver;

/**
 * Driver entity
 * @version 1.0
 * @author Viet Bui, Dany Gunawan
 * @lastmodify 2009-11-05 (Dany Gunawan)
 */
import au.edu.swin.swinvehicle.driverlicencetype.DriverLicenceTypeEntity;
import au.edu.swin.swinvehicle.vehiclebooking.VehicleBookingEntity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "DRIVER")
@NamedQueries({
    @NamedQuery(name = "findAllDrivers",
    query = "SELECT d FROM DriverEntity d")
})
public class DriverEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column(length = 60, unique = false, nullable = true)
    private String firstName;
    @Column(length = 60, unique = false, nullable = true)
    private String lastName;
    @Column(length = 60, unique = true, nullable = false)
    private String userName;
    @Column(length = 60, unique = false, nullable = false)
    private String emailAddress;
    @Column(length = 20, unique = false, nullable = false)
    private String contactNumber;
    @Column(length = 255, unique = false, nullable = false)
    private String homeAddress;
    @OneToMany(mappedBy = "driver", cascade = CascadeType.ALL)
    private List<VehicleBookingEntity> vehicleBookings;
    @ManyToOne(optional = false, cascade = CascadeType.PERSIST)
    private DriverLicenceTypeEntity driverLicenceType;

    /**
     * Default constructor
     */
    public DriverEntity() {
        vehicleBookings = new ArrayList<VehicleBookingEntity>();
    }

    public DriverEntity(String username, String comment, DriverLicenceTypeEntity driverLicenceType) {
        this(); // Call default constructor
        setUserName(username);

        setDriverLicenceType(driverLicenceType);
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public List<VehicleBookingEntity> getVehicleBookings() {
        return vehicleBookings;
    }

    public void setVehicleBookings(List<VehicleBookingEntity> vehicleBookings) {
        this.vehicleBookings = vehicleBookings;
    }

    public DriverLicenceTypeEntity getDriverLicenceType() {
        return driverLicenceType;
    }

    public void setDriverLicenceType(DriverLicenceTypeEntity driverLicenceType) {
        this.driverLicenceType = driverLicenceType;
    }

    public String getContactNumber() {
        return contactNumber;
    }

    public void setContactNumber(String contactNumber) {
        this.contactNumber = contactNumber;
    }

    public String getEmailAddress() {
        return emailAddress;
    }

    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getHomeAddress() {
        return homeAddress;
    }

    public void setHomeAddress(String homeAddress) {
        this.homeAddress = homeAddress;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    //Transfer object
    public DriverDetailsTO getDetailsData() {
        DriverDetailsTO driverDetailsTO = new DriverDetailsTO();


        driverDetailsTO.setContactNumber(this.getContactNumber());
        driverDetailsTO.setId(this.getId());
        driverDetailsTO.setUsername(this.getUserName());

        driverDetailsTO.setEmailAddress(this.getEmailAddress());
        driverDetailsTO.setFirstName(this.getFirstName());
        driverDetailsTO.setLastName(this.getLastName());
        driverDetailsTO.setHomeAddress(this.getHomeAddress());
        driverDetailsTO.setDriverLicenceType(this.getDriverLicenceType());
        driverDetailsTO.setVehicleBookings(this.getVehicleBookings());

        return driverDetailsTO;
    }

    public DriverTO getData() {
        DriverTO driverTO = new DriverTO();

        driverTO.setId(this.getId());
        driverTO.setUsername(this.getUserName());

        return driverTO;
    }

    public void setDetailsData(DriverDetailsTO to) {

        setId(to.getId());
        setUserName(to.getUsername());
        setContactNumber(to.getContactNumber());
        setEmailAddress(to.getEmailAddress());
        setFirstName(to.getFirstName());
        setLastName(to.getLastName());
        setHomeAddress(to.getHomeAddress());
        setDriverLicenceType(to.getDriverLicenceType());
        this.setVehicleBookings(to.getVehicleBookings());
    }

    public void setData(DriverTO to) {
        setId(to.getId());
        setUserName(to.getUsername());
    }

    @Override
    public String toString() {
        return "au.edu.swin.swinvehicle.driver.DriverEntity[id=" + id + "]";
    }
}
